<?php
/**
 * Cleaner of formatted text
 * 
 * PHP version 5
 *
 * Copyright (c) 2009 Anthony Willis
 * 
 * LICENSE:
 * 
 * This library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any
 * later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @category  ToolsAndUtilities
 * @package   textCleaner
 * @author    Anthony Willis <antwillis@gmail.com>
 * @copyright 2009 Anthony Willis
 * @license   http://www.gnu.org/licenses/gpl-1.0.html
 * @version   0.1
 * @filesource
 * @link      http://code.google.com/p/phptextcleaner
 * @since     0.1
 * @todo      Finalise documentation
 */

include_once('./classes/class.form.php');
include_once('./classes/class.textCleaner.php');
include_once('./classes/class.textCleaner.form.php');

$form = new form($_SERVER['REQUEST_METHOD']);

$fieldsets = array('old', 'options', 'new');

foreach ($fieldsets as $set) {
	$fieldset[$set]['state'] = ' closed';
	$fieldset[$set]['display'] = ' style="display:none;"';
}

$cleaner = new textCleanerForm();

$output = array('original_text' => '', 'new_text' => '', 'notice' => '');

if ($_SERVER['REQUEST_METHOD'] == "POST") {

	$processed =
		$cleaner->clean(
			$form->set_variable('original', '', false),
			$form->set_variable('break_type', 'none'),
			$form->set_variable('doc_type', 'html'),
			$form->set_variable('encoding', 'utf-8'),
			$form->set_variable('html', 'false'),
			$form->set_variable('links', 0),
			$form->set_variable('start_tag'),
			$form->set_variable('end_tag'),
			true
		);

	$output['original_text'] = $processed['original_text'];

	if (count($processed['errors']) > 0) {
		$output['notice'] = "<div class=\"notice error\">\n"
			. "<h3>Errors detected</h3>\n<ul>\n";
			
		foreach($processed['errors'] as $msg) {
			$output['notice'] .= "<li>".$msg."</li>\n";
		}
		
		$output['notice'] .= "</ul>\n</div>\n";
		
		$fieldset['old']['state'] = '';
		$fieldset['old']['display'] = '';
		$fieldset['options']['state'] = '';
		$fieldset['options']['display'] = '';
		
	} else {
		$output['new_text'] =
			$form->textarea(
				'new_text',
				$processed['new_text'],
				$cleaner->textarea_new_text_attr
			);
			
		$output['notice'] = "<div class=\"notice success\">\n"
			."<h3>Clean completed</h3>\n<ul>\n";
		$output['notice'] .= "<li>Your text was successfully cleaned and the "
			. "output is displayed below.</li>\n</ul>\n</div>\n";
			
		$fieldset['new']['state'] = '';
		$fieldset['new']['display'] = '';
	}

} else {
	$fieldset['old']['state'] = '';
	$fieldset['old']['display'] = '';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
		<title>Clean Word Documents for HTML</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=<?php print $form->set_variable('encoding', 'utf-8'); ?>" />
		<link href="./style/core.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript" src="./scripts/js/jquery.js"></script>
		<script type="text/javascript" src="./scripts/js/jquery.textarearesizer.js"></script>
		<script type="text/javascript" src="./scripts/js/cleaner.js"></script>
	</head>
	<body>
		<div id="header">
			<h1>Text cleaner 2.0 <span id="alpha">alpha</span></h1>
			<p>Clean up text from Microsoft Word documents, replacing HTML unfriendly characters with their HTML friendly equivalents.</p>
		</div>
		<div id="main">
			<form name="parser" id="parser" method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
				<?php print $output['notice']; ?>
				<fieldset id="old">
					<legend class="toggle<?php print $fieldset['old']['state']; ?>">Step 1: Original Text</legend>
					<p class="instruction">Paste original MS Word text here</p>
					<div class="container"<?php print $fieldset['old']['display']; ?>>
						<?php print $form->textarea('original', $output['original_text'], $cleaner->textarea_original_attr); ?>
					</div>
				</fieldset>
				<fieldset id="options">
					<legend class="toggle<?php print $fieldset['options']['state']; ?>">Step 2: Options</legend>
					<p class="instruction">Select the way you would like your text processed</p>
					<div class="container"<?php print $fieldset['options']['display']; ?>>
						<div class="options">
							<?php
								print $form->checkbox('html', $cleaner->html_options);
								print $form->select('links', $cleaner->links_options, $cleaner->links_label);
								print $form->select('encoding', $cleaner->encoding_options, $cleaner->encoding_label);
							?>
						</div>
						<div class="options">
							<?php
								print $form->select('break_type', $cleaner->break_types, $cleaner->break_types_label);
								print $form->select('doc_type', $cleaner->doc_types, $cleaner->doc_types_label);
							?>
						</div>
						<div class="options">
							<span class="labelhead">Custom tags</span>
							<?php
								print $form->textfield('start_tag', $cleaner->start_tag, $cleaner->start_tag_label);
								print $form->textfield('end_tag', $cleaner->end_tag, $cleaner->end_tag_label);
							?>
						</div>
					</div>
				</fieldset>
				<fieldset id="new">
					<legend class="toggle<?php print $fieldset['new']['state']; ?>">Step 3: Clean Text</legend>
					<p class="instruction">Clean up your text <?php print($form->submit('submit', 'CLEAN IT UP!', array('id' => 'textasubmit'))); ?></p>
					<div class="container"<?php print $fieldset['new']['display']; ?>>
						<p class="comment">Copy clean text from here (try using CTRL+A for Windows and Unix or CMD+A for OSX to select all the text)</p>
						<?php print($output['new_text']); ?>
					</div>
				</fieldset>
			</form>
		</div>
		<div id="foot">
			<p><a href="http://www.anthonywillis.com">Support</a> | &copy; 2009 <a href="http://www.anthonywillis.com">Anthony Willis</a></p>
		</div>
	</body>
</html>